﻿using System;

namespace Guru.Framework.Extensions
{
    public static class ObjectExtension
    {
        public static bool IsNull(this object o)
        {
            return o == null;
        }

        public static bool IsNullOrDbNull(this object obj)
        {
            return (null == obj | obj == DBNull.Value);
        }

        public static string ToStr(this object obj)
        {
            return obj.IsNull() == true ? string.Empty : obj.ToString();
        }

        public static int ToInt(this object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static int Str2Int(this string str)
        {
            try
            {
                return int.Parse(str);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
